import {observable, action, computed} from 'mobx';
import * as goalLevel from '../../api/goal/goalLevel'
import { Tools ,SearchBar,Icon} from 'weaver-mobile';
import {ListViewPage, SearchAdvanced} from 'weaver-mobile-page';
const getLabel = Tools.getLabel;
export default class GoalLevelStore {

    @observable loading = true; // 页面初始化的loading状态：数据加载成功前后前使用
    @observable goalInfoList = [] //卡片切换的目标数据
    @observable rootname = ""; //根节点名称
    @observable list = new ListViewPage.Store({route: '/userCenter/blogAttention/attention/c'});
    @observable search = new SearchAdvanced.Store({route: '/performance'});
    @observable currentkey = ""; //当前点击节点
    @observable tagLevelList = []; //标签层的列表
    @observable iscgoal = true; //是否开放
    @observable msg = "";
    /**
     * 联动组件条件
     */
    @observable nowyear = "";//当前年
    @observable nowmonth = "";//当前月
    @observable nowseason = "";  //当前季度
    @observable currentperiod = "3";//当前周期 3-年度

    //组织目标
    @observable condtype = ""  //当为2时为公司目标
    @observable typeId = ""; //目标类型 1公司目标 2分部目标 3部门目标
    @observable orgId = ""; //组织id


    @action
    setOrgData = (condtype,typeId,orgId) =>{
        this.condtype = condtype;
        this.typeId = typeId;
        this.orgId = orgId;
    }

    @action
    setTagLevelList = (data) =>{
        this.tagLevelList = data;
    }

    @action("卡片切换的目标数据")
    goalCardList = (params) => {
        this.loading = true;
        this.getCondition(params);
        goalLevel.goalCardList(params).then(action(res => {   
            if(res.status){
                this.goalInfoList = res.goalInfoList;
                this.rootname = res.rootname;
                this.currentkey = params.supid;
                this.iscgoal = res.iscgoal;
                this.msg = res.msg;
                if(!params.supid || params.supid == ""){
                    this.tagLevelList = [{id:"",name:res.rootname}];
                }
            }else{
                // message.error(res.msg || getLabel('25009','失败'));
            }
            this.loading = false;
        }));
    }

    @action("获取目标列表的值")
    goalListPage = (params) => {
        this.loading = true;
        this.getCondition(params);
        goalLevel.goalListPage(params).then(action(res => {
            this.list.clearData();
            if (res.api_status) {
                const {datas: dataKey} = res;
               this.list.getData({dataKey});
            } else {

            }
            this.loading = false;
        }));

    }

    @action("列表点击")
    onClickItem = (params) => {
        this.tagLevelList.push({id:params.supid,name:params.name});
        this.goalCardList(params);
        if(params.supid == ""){
            this.list.clearData();
        }else{
            this.currentkey = params.supid;
            this.goalListPage(params);
        }
    }

    @action("卡片切换")
    onCardSwitch = (currentkey,currentName) =>{
        this.currentkey = currentkey;
        if(this.tagLevelList.length > 0){
            this.tagLevelList[this.tagLevelList.length - 1] = {id:currentkey,name:currentName}
        }
    }

    @action("标签切换")
    onTagSwitch = (id,list) =>{
        let params = {supid:id}
        if(id == ""){ //查询顶级公司目标
            params.condtype = this.condtype;
        }
        this.goalCardList(params);
        this.tagLevelList = list;
        this.goalListPage(params);
    }

    @action
    setCondType = (v) =>{
        this.condtype = v;
    }

    @action
    getCondition = (params) =>{
        params.period = this.currentperiod;
        params.periody = this.nowyear;
        params.periodm = this.nowmonth;
        params.periods = this.nowseason;
        params.condtype = this.condtype;
        params.typeId = this.typeId;
        params.orgId = this.orgId;
    }

    @action("更改查询条件")
    setCondition = (dateParams) =>{
        this.currentperiod = dateParams.period;
        this.nowyear = dateParams.year;
        this.nowmonth = dateParams.month;
        this.nowseason = dateParams.quarter;
        this.currentOrgId = dateParams.currentOrgId;
        this.typeId = dateParams.typeId;
        this.orgId = dateParams.orgId;
        this.resetDataList();
        this.goalInfoList = [];
        this.tagLevelList = [];
    }

    @action("重置数据")
    resetDataList = () =>{
        let params = {};
        // params.supid = this.currentkey;
        this.goalListPage(params)
    }

    @action
    changeCondtype = (condtype) =>{
        this.condtype = condtype;
        this.goalListPage({condtype})
    }

}